from pymt import *


class MTAddButton(MTImageButton):

  def __init__(self, function, **kwargs):
    self.function=function
    super(MTAddButton, self).__init__(**kwargs)
	
  def on_release(self, touch):
    self.function()

class MTMiniature(MTButton):

  def __init__(self,id,parent, **kwargs):
	kwargs.setdefault('label', id+1)
	self.__status="open"
	self.w = parent
	self.__id=id
	self.__emplacement=id
	self.__on_move=False
	self.__locked=False
	self.hold=False
	super(MTMiniature, self).__init__(**kwargs)
	
  def on_release(self, touch):
    # if the touch isn't on the widget we do nothing
	if not self.collide_point(touch.x, touch.y):
		return False 
	if self.__locked==False:
		self.w.unlock()
	if 	self.__on_move:
		self.pos=110+(self._get_emplacement())*110,15
		self.hold=False		
		self.__on_move=False
		return True
	else:
		if self.__status=="close":
			self.w.openPage(self.__id)
			self.__status="open"
			return True
		if self.__status=="open":
			self.w.closePage(self.__id)
			self.__status="close"		
			return True
	
  def on_press(self, touch):
	if self.__locked==True:
		return False
	# if the touch isn't on the widget we do nothing
	if not self.collide_point(touch.x, touch.y):
		return False
	else:
		self.bring_to_front()	
		return True
		
  def on_touch_move(self, touch):
	if self.__locked==True:
		return False	# if the touch isn't on the widget we do nothing
	if not self.collide_point(touch.x, touch.y):
		return False
	else:
		if(touch.is_held and (touch.xmot == 0 and touch.ymot == 0)):
			self.hold=True
        if(self.hold==True):
			self.w.lockOthers(self.__id)
			self.bring_to_front()
			self.__on_move=True	
			self.center=(touch.x, touch.y)
		
			self.w.moveMiniature(self.__id)
		
			return MTButton.on_touch_move(self, touch)
	
  def close(self):
    self.__status="close"

  def lock(self):
    self.__locked=True

  def unlock(self):
    self.__locked=False	

  def draw(self):
    if self.__status == "close":
        self.cls="miniatureClose"
    else:
        self.cls="miniature"
    MTButton.draw(self)

  def _get_emplacement(self):
    return self.__emplacement
  def _set_emplacement(self, emplacement):
	self.__emplacement=emplacement
	return
  emplacement = property(_get_emplacement, _set_emplacement)
  
  def _get_id(self):
    return self.__id
  id = property(_get_id) 
	
	
 